﻿
using KSOAdmin.Core.Log;
using KSOAdmin.Core.Other;
using KSOAdmin.Models.Core;

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;

namespace KSOAdmin.Extensions.ApiFilter
{
    public class ApiExceptionFilterAttribute : IExceptionFilter
    {
      
        public ApiExceptionFilterAttribute( )
        {
        }

        public void OnException(ExceptionContext context)
        {
            if (!context.ExceptionHandled)
            {
                string Msg= context.Exception.Message;
                var url= context.HttpContext.Request.GetAbsoluteUri();

                //日志
                Logging.Error(Models.MapperModels.Enums.LoggerType.Error, url, Msg);
                Logging.WriteError(context.HttpContext.Request.Path, UserObtain.UserId);
                context.Result = new JsonResult(new ResponseModel()
                {
                    msg = "错误"+Msg,
                    code=500,
                    data=null,
                    isSuccess=true
                });

            }
            context.ExceptionHandled = true;
        }
    }
}
